package com.ysh.smartplatform.dto;

import com.ysh.smartplatform.constans.ResultCode;
import com.ysh.smartplatform.pojo.CheckNull;
import com.ysh.smartplatform.valigroup.Insert;
import com.ysh.smartplatform.valigroup.Select;
import lombok.Data;
import lombok.ToString;
import org.hibernate.validator.constraints.Length;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;

/**
 * 袁胜虎 on 2022/9/5 11:35
 */
@Data
@ToString
public class UserDto implements CheckNull {
    //group指的是当前校验的时候指定的group属于当前groups中的一个的时候就进行校验   用json格式添加message
    @NotEmpty(message = "{\"code\": "+ ResultCode.PARAM_CONTENT_EMPTY +",\"msg\": \"用户名不能为空,请检查\"}", groups = {Insert.class, Select.class})
    private String username;
    @Length(min = 6, max = 10 ,message = "{\"code\": "+ ResultCode.PARAM_CONTENT_PARTTEN_ERROR +",\"msg\": \"密码长度6-10\"}", groups ={Insert.class, Select.class})
    private String password;

    @NotEmpty(message = "{\"code\": "+ ResultCode.PARAM_CONTENT_EMPTY +",\"msg\": \"必须传递昵称\"}", groups = Insert.class)
    private String name;

    @Pattern(regexp = "^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\\d{8}$", groups = Insert.class,message = "{\"code\": "+ ResultCode.PARAM_CONTENT_PARTTEN_ERROR +",\"msg\": \"手机号错误\"}")
    private String phone;

    @Pattern(regexp = "^\\w+@[\\da-z\\.-]+\\.([a-z]{2,6}|[\\u2E80-\\u9FFF]{2,3})$", groups = Insert.class,message = "{\"code\": "+ ResultCode.PARAM_CONTENT_PARTTEN_ERROR +",\"msg\": \"邮箱错误\"}")
    private String email;

    @NotNull(groups = Insert.class,message = "{\"code\": "+ ResultCode.PARAM_CONTENT_EMPTY +",\"msg\": \"性别未传递\"}")
    private Long sex;


    @Override
    public boolean isEmpty() {
        return !StringUtils.hasText(username) ||
                !StringUtils.hasText(password) ||
                !StringUtils.hasText(name) ||
                !StringUtils.hasText(phone) ||
                !StringUtils.hasText(email) ||
                ObjectUtils.isEmpty(sex)||
                (sex != 0 & sex != 1);
    }
}
